﻿using BerkeliumDotNet;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace FormsDemo
{
    public partial class WebForm : Form
    {
        string URL = "http://www.google.com";

        Window webWindow;
        Timer timer;

        public PictureBox WebBox
        {
            get { return webFormBitmap; }
        }

        public WebForm()
        {
            InitializeComponent();

            FormClosed += WebForm_FormClosed;
            Resize += WebForm_Resize;
            WebBox.MouseMove += WebBox_MouseMove;
            WebBox.MouseDown += WebBox_MouseDown;
            WebBox.MouseUp += WebBox_MouseUp;
            MouseWheel += WebForm_MouseWheel;
            GetResizedBitmap();

            BerkeliumDotNet.Berkelium.Init();

            webWindow = Window.Create();
            webWindow.SetTransparent(false);
            webWindow.Resize(WebBox.Width, WebBox.Height);
            webWindow.SetDelegate(new WebListener(this));
            webWindow.NavigateTo(URL);

            timer = new Timer();
            timer.Interval = 30;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();
        }

        void WebForm_MouseWheel(object sender, MouseEventArgs e)
        {
            webWindow.MouseWheel(0, e.Delta);
        }

        void WebBox_MouseDown(object sender, MouseEventArgs e)
        {
            webWindow.MouseMoved(e.X, e.Y);
            webWindow.MouseButton(0, true);
        }

        void WebBox_MouseUp(object sender, MouseEventArgs e)
        {
            webWindow.MouseMoved(e.X, e.Y);
            webWindow.MouseButton(0, false);
        }

        void WebBox_MouseMove(object sender, MouseEventArgs e)
        {
            webWindow.MouseMoved(e.X, e.Y);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            BerkeliumDotNet.Berkelium.Update();
        }

        void WebForm_Resize(object sender, EventArgs e)
        {
            GetResizedBitmap();
            webWindow.Resize(WebBox.Width, WebBox.Height);
        }

        void GetResizedBitmap()
        {
            if (WebBox.Height == 0)
                return;

            WebBox.Image = new Bitmap(WebBox.Width, WebBox.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
        }

        void WebForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            webWindow.Dispose();
            BerkeliumDotNet.Berkelium.Destroy();
        }
    }

    class WebListener : WindowDelegate
    {
        WebForm webForm;

        public WebListener(WebForm webForm)
        {
            this.webForm = webForm;
        }

        public override void OnPaint(Window window, IntPtr bitmapIn, Rectangle bitmapRect, int dx, int dy, Rectangle scrollRect)
        {
            //if (dy != 0)
            //    return;

            BerkeliumDotNet.Berkelium.RenderToBitmap(
                (Bitmap)webForm.WebBox.Image, bitmapIn, bitmapRect);
            webForm.WebBox.Invalidate();
        }
    };
}
